﻿using FangChanCrm.Services.Interface;
using FangChanCrmEF;
using FangChanCrmModel;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Dynamic.Core;
using System.Threading.Tasks;

namespace FangChanCrm.Services.Realization
{
    public class t_XinWenBiaoService : It_XinWenBiaoService
    {
        public readonly MyContext _Context;

        public t_XinWenBiaoService(MyContext context)
        {
            _Context = context;
        }

        public t_XinWenBiao GetId(int id)
        {
            return _Context.t_XinWenBiao.FirstOrDefault(x => x.Id == id);
        }

        public int XinWenAdd(t_XinWenBiao XinWen)
        {
            _Context.t_XinWenBiao.Add(XinWen);
            return _Context.SaveChanges();
        }

        public string ReturnWhere(t_XinWenBiao t_XinWenBiao)
        {
            string returnstr = "1 = 1";
            if (t_XinWenBiao.BeiZhu1 != null)
            {
                returnstr = returnstr + " and BeiZhu1 == " + t_XinWenBiao.BeiZhu1 + "";
            }
            if (t_XinWenBiao.BeiZhu2 != null)
            {
                returnstr = returnstr + " and BeiZhu2.Contains(" + t_XinWenBiao.BeiZhu2 + ")";
            }
            if (t_XinWenBiao.BeiZhu3 != null)
            {
                returnstr = returnstr + " and BeiZhu3.Contains(" + t_XinWenBiao.BeiZhu3 + ")";
            }
            if (t_XinWenBiao.BeiZhu4 != null)
            {
                returnstr = returnstr + " and BeiZhu4.Contains(" + t_XinWenBiao.BeiZhu4 + ")";
            }
            if (t_XinWenBiao.BeiZhu5 != null)
            {
                returnstr = returnstr + " and BeiZhu5.Contains(" + t_XinWenBiao.BeiZhu5 + ")";
            }
            if (t_XinWenBiao.BeiZhu6 != null)
            {
                returnstr = returnstr + " and BeiZhu6.Contains(" + t_XinWenBiao.BeiZhu6 + ")";
            }
            if (t_XinWenBiao.BeiZhu7 != null)
            {
                returnstr = returnstr + " and BeiZhu7.Contains(" + t_XinWenBiao.BeiZhu7 + ")";
            }
            if (t_XinWenBiao.BeiZhu8 != null)
            {
                returnstr = returnstr + " and BeiZhu8.Contains(" + t_XinWenBiao.BeiZhu8 + ")";
            }
            if (t_XinWenBiao.BeiZhu9 != null)
            {
                returnstr = returnstr + " and BeiZhu9.Contains(" + t_XinWenBiao.BeiZhu9 + ")";
            }
            if (t_XinWenBiao.BeiZhu10 != null)
            {
                returnstr = returnstr + " and BeiZhu10.Contains(" + t_XinWenBiao.BeiZhu10 + ")";
            }
            if (t_XinWenBiao.FangBuRenId != 0)
            {
                returnstr = returnstr + " and FangBuRenId == "+ t_XinWenBiao.FangBuRenId + "";
            }
            if (t_XinWenBiao.XinWenNeiRong != null)
            {
                returnstr = returnstr + " and XinWenNeiRong.Contains(" + t_XinWenBiao.XinWenNeiRong + ")";
            }
            if (t_XinWenBiao.ZhuangTai != null)
            {
                returnstr = returnstr + " and ZhuangTai.Contains(" + t_XinWenBiao.ZhuangTai + ")";
            }
            if (t_XinWenBiao.FaBuShiJian != null)
            {
                returnstr = returnstr + " and FaBuShiJian.Contains(" + t_XinWenBiao.FaBuShiJian + ")";
            }
            if (t_XinWenBiao.XinWenDengJi != null)
            {
                returnstr = returnstr + " and XinWenDengJi.Contains(" + t_XinWenBiao.XinWenDengJi + ")";
            }
            return returnstr;
        }

       

        public List<t_XinWenBiao> GetXinWenList()
        {
            return _Context.t_XinWenBiao.DistinctBy(x => x.FaBuShiJian).OrderByDescending(x => x.FaBuShiJian).ToList();
        }

        public string GetListWithFenYe(t_XinWenBiao t_XinWenBiao, int start, int limit, string dir, int column)
        {
            var returnwhere = ReturnWhere(t_XinWenBiao);
            var list = new List<t_XinWenBiao>();
            if (returnwhere.Length > 0)
            {
                list = _Context.t_XinWenBiao.Where(returnwhere).ToList();
            }
            else
            {
                list = _Context.t_XinWenBiao.ToList();
            }
            switch (column)
            {
                case 0:
                    if (dir == "asc")
                    {
                        list = list.OrderBy(x => x.BeiZhu1).ToList();
                    }
                    else
                    {
                        list = list.OrderByDescending(x => x.BeiZhu1).ToList();
                    }
                    break;
                case 1:
                    if (dir == "asc")
                    {
                        list = list.OrderBy(x => Convert.ToDateTime(x.FaBuShiJian)).ToList();
                    }
                    else
                    {
                        list = list.OrderByDescending(x => Convert.ToDateTime(x.FaBuShiJian)).ToList();
                    }
                    break;
                default:
                    break;
            }
            return Helper.ListToJsonAddFenYe(list, start, limit);
        }
    }
}
